package com.china08.yxyapi.api.manage.edu.platform.resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源管理 搜索
 * 
 */
@CrossOrigin
@RestController("EduResourceQualityController")
public class ResourceQualityController extends YxyApiBaseController {

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @ApiOperation(value = "教育局 查询资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/quality", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        String msg = "设置精品成功";
        if (reqModel.quality == 0) {
            msg = "取消精品成功";
        }
        YxyResResource resource = yxyResResourceRepository.findById(reqModel.resourceId);
        Assert.notNull(resource, "资源未找到，请刷新页面");
        resource.setIsHighQualityQy(reqModel.quality);
        yxyResResourceRepository.save(resource);
        EsResourceUpdateUtil.updateEsResoure(reqModel.resourceId);
        return new YxyApiRespModel(msg);
    }

    public static class ReqModel extends YxyApiPageReqModel {
        // 区域代
        public String resourceId;

        // 0 取消 1加精
        public Integer quality;
    }
}
